<template>
  <view class="page-transition" :class="transitionClass">
    <slot></slot>
  </view>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';

@Component
export default class PageTransition extends Vue {
  @Prop({ default: 'fade' }) type!: 'fade' | 'slide-left' | 'slide-right' | 'slide-up' | 'slide-down' | 'zoom' | 'flip';
  @Prop({ default: 300 }) duration!: number;
  @Prop({ default: false }) show!: boolean;
  
  transitionClass: string = '';
  
  mounted() {
    this.updateTransition();
  }
  
  @Watch('show')
  onShowChange() {
    this.updateTransition();
  }
  
  @Watch('type')
  onTypeChange() {
    this.updateTransition();
  }
  
  /**
   * 更新过渡动画类
   */
  updateTransition() {
    this.transitionClass = `${this.type} ${this.show ? 'enter' : 'leave'}`;
  }
  
  /**
   * 进入动画
   */
  enter() {
    this.transitionClass = `${this.type} enter`;
  }
  
  /**
   * 离开动画
   */
  leave() {
    this.transitionClass = `${this.type} leave`;
  }
}
</script>

<style lang="scss" scoped>
.page-transition {
  width: 100%;
  height: 100%;
  transition-property: transform, opacity;
  transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  
  // 淡入淡出
  &.fade {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: translateZ(0);
    }
    
    &.leave {
      opacity: 0;
      transform: translateZ(0);
    }
  }
  
  // 左滑
  &.slide-left {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: translateX(0);
    }
    
    &.leave {
      opacity: 0;
      transform: translateX(-100%);
    }
  }
  
  // 右滑
  &.slide-right {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: translateX(0);
    }
    
    &.leave {
      opacity: 0;
      transform: translateX(100%);
    }
  }
  
  // 上滑
  &.slide-up {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: translateY(0);
    }
    
    &.leave {
      opacity: 0;
      transform: translateY(-100%);
    }
  }
  
  // 下滑
  &.slide-down {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: translateY(0);
    }
    
    &.leave {
      opacity: 0;
      transform: translateY(100%);
    }
  }
  
  // 缩放
  &.zoom {
    transition-duration: 0.3s;
    
    &.enter {
      opacity: 1;
      transform: scale(1);
    }
    
    &.leave {
      opacity: 0;
      transform: scale(0.8);
    }
  }
  
  // 翻转
  &.flip {
    transition-duration: 0.6s;
    transform-style: preserve-3d;
    
    &.enter {
      opacity: 1;
      transform: rotateY(0deg);
    }
    
    &.leave {
      opacity: 0;
      transform: rotateY(180deg);
    }
  }
}

// 弹性动画
.bounce-enter {
  animation: bounce-in 0.5s;
}

.bounce-leave {
  animation: bounce-out 0.5s;
}

@keyframes bounce-in {
  0% {
    transform: scale(0.3);
    opacity: 0;
  }
  50% {
    transform: scale(1.05);
  }
  70% {
    transform: scale(0.9);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes bounce-out {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0.3);
    opacity: 0;
  }
}

// 摇摆动画
.shake {
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% {
    transform: translateX(0);
  }
  10%, 30%, 50%, 70%, 90% {
    transform: translateX(-10rpx);
  }
  20%, 40%, 60%, 80% {
    transform: translateX(10rpx);
  }
}

// 脉冲动画
.pulse {
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

// 呼吸动画
.breathe {
  animation: breathe 2s ease-in-out infinite;
}

@keyframes breathe {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.02);
    opacity: 0.8;
  }
}